package com.newlink.building.userinfo.activity

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.newlink.building.common_base.base.NL_BaseActivity
import com.newlink.building.userinfo.R
import com.newlink.building.userinfo.databinding.ActivityNotifySettingBinding
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationHelper
import com.newlink.building.obfuscation.Module_Obfuscation_ObfuscationInvoker

/**
 * @author wangyang
 * @date 2022/9/19
 * @description 我的-通话设置页面
 */
class Module_UserInfo_NotifySettingActivityNL :
    NL_BaseActivity(), View.OnClickListener {

//    override fun attachLayoutRes(): Int {
//        return R.layout.activity_notify_setting
//    }

    private lateinit var mBinding: ActivityNotifySettingBinding
    override fun attachLayoutRes(): View {
        mBinding = ActivityNotifySettingBinding.inflate(layoutInflater)
        return mBinding.root
    }

    override fun initData() {
    }

    override fun initView() {
        // 通知设置页面初始化时的混淆
        Module_Obfuscation_ObfuscationHelper.onMethodEnter("NotifySettingActivity", "initView")
        Module_Obfuscation_ObfuscationInvoker.invokeRandom("notify_setting")

        findViewById<TextView>(R.id.tv_title_bar_center).text =
            resources.getText(R.string.notify_setting)

        findViewById<View>(R.id.notify_system_ongoing).setOnClickListener(
            this@Module_UserInfo_NotifySettingActivityNL
        )
        findViewById<View>(R.id.notify_system_all_permission).setOnClickListener(
            this@Module_UserInfo_NotifySettingActivityNL
        )
    }

    private fun goToAndroidNotificationSettings() {
        if (Build.VERSION.SDK_INT >= 26) {
            val i = Intent()
            i.action = Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS
            i.putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
            i.putExtra(
                Settings.EXTRA_CHANNEL_ID,
                getString(R.string.notification_channel_service_id)
            )
            i.addCategory(Intent.CATEGORY_DEFAULT)
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
            i.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
            startActivity(i)
        }


    }

    private fun goToApplicationPermission() {
        val intent = Intent()
        intent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
        intent.addCategory(Intent.CATEGORY_DEFAULT)
        intent.data = Uri.parse("package:${packageName}")
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
        ContextCompat.startActivity(this, intent, null)
    }

    override fun onClick(v: View) {
        // 点击事件处理前的混淆
        Module_Obfuscation_ObfuscationInvoker.invokeSpecific(14, v.id)

        when (v.id) {
            R.id.notify_system_ongoing -> goToAndroidNotificationSettings()
            R.id.notify_system_all_permission -> goToApplicationPermission()
        }
    }


}